# -*- encoding: UTF-8 -*-
'''
@File    :  main.py
@Time    :  2021/02/27 23:18:17
@Author  :  Kearney
@Contact :  191615342@qq.com
@Desc    :  定时触发脚本
'''
import schedule
import time
import datetime
import os
import requests
from mail import sendMail


def job():
    try:
        r = requests.get('http://open.iciba.com/dsapi/').json()
        sendMail(qq=os.getenv('QQ'),
                pwd=os.getenv('PWD'),
                receiver=os.getenv('MAIL_RECEIVER'),
                mail_content=r['note'])
        with open('love.txt', 'a') as f:
            f.write(str(datetime.datetime.now()) + '\t' + r['note'] + '\n')
    except Exception as e:  # 异常记录到日至文件log.txt
        print("Exception occur, see in the log.txt")
        with open('log.txt', 'a') as f:
            f.write(str(e) + '\n')

schedule.every().day.at("10:00").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
